//
//  HUMOpenGLRender1.swift
//  OpenGLDemo1
//
//  Created by Ming on 2018/10/8.
//  Copyright © 2018年 Ming. All rights reserved.
//

import UIKit

class HUMOpenGLRender1: HUMOpenGLBaseRender {
    private var shader: HUMOpenGLShader?
    
    private var r: GLfloat = 0, g: GLfloat = 0, b: GLfloat = 0
    
    override init?() {
        super.init()
        let vertextShaderPath = Bundle.main.path(forResource: "vertext_shader_1", ofType: "vert")
        let fragmentShaderPath = Bundle.main.path(forResource: "fragment_shader_1", ofType: "frag")
        shader = HUMOpenGLShader(vertextShaderPath: vertextShaderPath, fragmentShaderPath: fragmentShaderPath)
    }
    
    override func render() {
        super.render()
        
        r += 1
        g += 1.25
        b += 1.5
        
        let color: [GLfloat] = [GLfloat(Int(r) % 255) / 255,  GLfloat(Int(g) % 255) / 255,  GLfloat(Int(b) % 255) / 255, 1.0]
        let vertexPos: [GLfloat] = [0.0, 0.5, 0.0,
                                    -0.5, -0.5, 0.0,
                                    0.5, -0.5, 0.0]
        shader?.use()
        glVertexAttrib4fv(0, color)
        glVertexAttribPointer(1, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, vertexPos)
        glEnableVertexAttribArray(1)
        glDrawArrays(GLenum(GL_TRIANGLES), 0, 3)
        glDisableVertexAttribArray(1)
    }
}
